
"""
Created on Tue Feb 19 14:55:43 2013

@author: aponteeduarddo@gmail.com
Copyright Eduardo Aponte 2013.
"""

from mex_binder import binder_py

eyelink_timed_command = binder_py('mx_eyelink_timed_command')
eyelink_timed_command.mx_c.headers = ['<eyelink.h>','"mex.h"','"utils.h"']
eyelink_timed_command.bindo += ['utils.o']
eyelink_timed_command.mx_c.code = \
"""
INT16 _main( UINT32 msec, char FARTYPE *text )
{
    return eyelink_timed_command( msec, text );
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    char *mb;
    int bt;

    mwSize td[2] = {1,1};
    double *status;
    
    
    /* Verify input arguments. */
    if(nrhs != 2 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 1 ) {
        mexErrMsgTxt("Too many output arguments.");
    }
    
    if ( isscalar( prhs[0] ) ){
        if ( returnscalar( prhs[0], &bt ) != 1 )
            mexErrMsgTxt("Complex data is not supported.");
        if (bt < 0)
            mexErrMsgTxt("msec should be positive.");                   
    } else {
        mexErrMsgTxt("msec should be a scalar."); 
    } 
    
    if ( !mxIsChar(prhs[1]) )
      mexErrMsgTxt( "text must be a string.");
      
    mb  = mxArrayToString(prhs[1]);
    
    if(mb == NULL) 
      mexErrMsgTxt("Could not convert input to string.");
      
    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status = mxGetPr(plhs[0]);    
    *status = (double ) _main( (UINT32 ) bt, mb ); 
    return;    
}

"""

eyelink_timed_command.mx_c.mdoc = \
"""function [ ] = mx_eyelink_timed_command( msec, text )
%MX_EYELINK_TIMED_COMMAND Sends a command to the connected eye tracker, wait 
% for reply.

% Remarks:
% If eyelink_send_command() is used, the text command will be executed, and a 
% result code returned that can be read with eyelink_command_result().
% Parameters:
% text 	String command to send.
% msec 	Maximum milliseconds to wait for reply.
% Returns:
% OK_RESULT (0) if OK.
% NO_REPLY if timed out.
% LINK_TERMINATED_RESULT if cannot send.
% other error codes represent tracker execution error.
%
"""
eyelink_timed_command.write_c()
eyelink_timed_command.write_m()
eyelink_timed_command.compile()
eyelink_timed_command.write_sh()

